/**
 * Created by Mark_ren on 2017/5/22.
 * 苟富贵,無相忘
 * Update by Mark_ren on 2017/5/23.
 * 今天心情不好，可能是因为阴天。
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();

var articleController = {

    /**
     *  危重症时间 急危e频道接口
     * @param req
     * @param res
     */
    getJWArticleList: function (req, res) {
        co(function () {
            var item = parseInt(req.query.item);
            var sort = parseInt(req.query.sort);
            var type = parseInt(req.query.type);
            var title = "";
            if (req.query.title != undefined && req.query.title != null) {
                title = req.query.title;
            }
            seq.sequelize.query("select a.id,a.title,a.title_pic as titlePic, a.description ,a.url as URL,a.top_time,a.create_time," +
                "count(c.id) as cCount from lm_article as a  left join lm_click_log as c " +
                "on c.title_id = a.ID where a.item = " + item + " and a.sort = " + sort + " and title like '%" + title + "%'" +
                " group by a.ID order by a.top_time DESC,a.create_time DESC").spread(function (result) {
                    res.json({ "success": true, "result": result });
                });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })
    },

    /**
     *  危重症时间 急危e频道接口 卒中e频道
     * @param req
     * @param res
     */
    getArticle: function (req, res) {
        co(function () {

            var params = {};
            var paramT = {};
            //根据id
            if (req.query.id != undefined) {
                params["id"] = req.query.id;
            }
            if (req.query.sort != undefined) {
                params["sort"] = req.query.sort;
            }
            if (req.query.type != undefined) {
                params["type"] = req.query.type;
            }
            if (req.query.item != undefined) {
                params["item"] = req.query.item;
            }

            paramT["where"] = params;
            paramT["order"] = [["topTime", 'DESC']];

            seq.articleModule.findOne(paramT).then(function (result) {
                res.json({ "success": true, "result": result });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },

    /**
     *  脑血管时间 卒中e频道接口
     * @param req
     * @param res
     */
    getZZArticleList: function (req, res) {
        co(function () {
            var item = parseInt(req.query.item);
            var sort = parseInt(req.query.sort);
            var title = "";
            if (req.query.title != undefined && req.query.title != null) {
                title = req.query.title;
            }
            seq.sequelize.query("select a.id,a.title,a.title_pic as titlePic,a.url as URL, a.description ,a.top_time,a.create_time," +
                "count(c.id) as cCount from lm_article as a  left join lm_click_log as c " +
                "on c.title_id = a.ID where a.item = " + item + " and a.sort = " + sort + "  and title like '%" + title + "%'" +
                " group by a.ID order by a.top_time DESC,a.create_time DESC").spread(function (result) {
                    res.json({ "success": true, "result": result });
                });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },

    /**
     * 学科建设 科研加油站/科室巡礼/联盒国
     * @param req
     * @param res
     */
    getKYArticleList: function (req, res) {
        co(function () {
            var item = parseInt(req.query.item);
            var sort = parseInt(req.query.sort);
            var types = parseInt(req.query.type);
            var type = "";
            var title = "";
            if (req.query.title != undefined && req.query.title != null) {
                title = req.query.title;
            }
            if (types != null && types != 0) {
                type = " and a.type = " + types;
            }

            seq.sequelize.query("select a.id,a.title,a.title_pic as titlePic,a.url as URL, a.description ,a.top_time,a.create_time," +
                "count(c.id) as cCount from lm_article as a  left join lm_click_log as c " +
                "on c.title_id = a.ID where a.item = " + item + " and a.sort = " + sort + type + " and title like '%" + title + "%'" +
                " group by a.ID order by a.top_time DESC,a.create_time DESC").spread(function (result) {
                    res.json({ "success": true, "result": result });
                });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },

    /**
     *  增加图文
     * @param req
     * @param res
     */
    addArticle: function (req, res) {
        co(function () {
            var uuid = util.getUUID();
            req.body.id = uuid;
            req.body.createTime = util.getNowTime;
            seq.articleModule.create(req.body).then(function (result) {
                res.status(200).json({
                    message: "添加成功",
                    result: result
                });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '添加失败' });
            console.log(e);
        })

    }
}
module.exports = articleController;